package org.example.finalsecurities.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.example.finalsecurities.entity.CrawlerLog;
import org.example.finalsecurities.entity.StockIndex;
import org.example.finalsecurities.mapper.StockIndexMapper;
import org.example.finalsecurities.service.CrawlerLogService;
import org.example.finalsecurities.service.StockIndexService;
import org.example.finalsecurities.utils.CrawlerUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 股票指数服务实现类
 * 功能：
 * 1. 获取所有股票指数
 * 2. 根据code获取股票指数
 * 3. 保存或更新股票指数
 * 4. 爬取所有中国指数并更新
 *
 * @author : [CaiJianbin]
 * @version : [v1.0]
 */
@Service
@Slf4j
public class StockIndexServiceImpl implements StockIndexService {

    @Autowired
    private StockIndexMapper stockIndexMapper;
    @Autowired
    private CrawlerLogService crawlerLogService;

    /**
     * @author CaiJianbin
     * @description 获取所有股票指数
     * @param
     * @return java.util.List<org.example.finalsecurities.entity.StockIndex>
     */
    @Override
    public List<StockIndex> getAllStockIndices() {
        return stockIndexMapper.selectAll();
    }

    /**
     * @author CaiJianbin
     * @description 通过code获取股票指数
     * @param code
     * @return org.example.finalsecurities.entity.StockIndex
     */
    @Override
    public StockIndex getStockIndexByCode(String code) {
        return stockIndexMapper.selectByCode(code);
    }

    /**
     * @author CaiJianbin
     * @description 保存或更新股票指数
     * @param stockIndex
     * @return void
     */
    @Override
    public void saveOrUpdateStockIndex(StockIndex stockIndex) {
        StockIndex existIndex = getStockIndexByCode(stockIndex.getCode());
        if (existIndex != null) {
            //更新数据
            stockIndex.setId(existIndex.getId());
            stockIndex.setCreateTime(existIndex.getCreateTime());
            stockIndexMapper.update(stockIndex);
        } else {
            //插入数据
            stockIndexMapper.insert(stockIndex);
        }
    }

    /**
     * @author CaiJianbin
     * @description 爬取所有中国指数并更新
     * @param
     * @return void
     */
    @Override
    @Transactional
    public void fetchAndUpdateChinaIndexes() {
        Date startTime = new Date();
        log.info("开始爬取股票数据: {}", startTime);

        // 构建请求参数
        CrawlerLog crawlerLog = new CrawlerLog();
        crawlerLog.setActionDesc("爬取中国指数");
        crawlerLog.setUrl("https://quote.eastmoney.com/center/qqzs.html");
        crawlerLog.setStartTime(startTime);
        // 爬取数据
        List<StockIndex> chinaIndexs = CrawlerUtil.crawlChinaIndex();
        for(StockIndex chinaIndex : chinaIndexs) {
            saveOrUpdateStockIndex(chinaIndex);
        }
        // 保存爬取日志
        Date endTime = new Date();

        crawlerLog.setEndTime(endTime);
        crawlerLog.setCreateTime(endTime);
        crawlerLog.setUpdateTime(endTime);

        crawlerLogService.saveLog(crawlerLog);
        log.info("爬取股票数据完成，本次爬虫日志: {}", crawlerLog);
    }
}